<?php
class Bricabox_model extends Model {
	function Bricabox_model()
	{
		parent::Model();
	}
	
	function save($id, $params)
	{
		if (!is_array($params) || !count($params))
			return true;
		
		$CI =& get_instance();
		$CI->load->helper('security');
		
		// make sure no blank params exist
		$ignore_blank_keys = array('custom_header_html', 'custom_css', 'privacy', 'allow_engagement', 'use_custom_domain', 'custom_domain', 'api_key_google_maps', 'members_must_apply', 'can_clone', 'clone_no_copy_theme', 'default_wiki_text');
		foreach ($params as $k => $v)
		{
			// todo: if param is custom_domain, check that:
			// 1. it's unique and unused or it's already in use by this bricabox
			if ($k == 'custom_domain' && strlen($v))
			{
				// todo
			}
			
			if (!in_array($k, $ignore_blank_keys) && !$v)
				return false;
				
			// sanitize input
			if ($k != 'custom_header_html')
				$params[$k] = encode_php_tags(xss_clean($v));
			else
				$params[$k] = encode_php_tags($v);
			
			//
			// manipulations must go below!
			//
			
			// sanitize color values
			if (strpos($k, 'color_for_') !== false)
				$params[$k] = strtolower(preg_replace('/[^a-f0-9]/i', 'f', $v));
		}
		
		$this->db->where('id', $id);
		if ($this->db->update('bricaboxes', $params))
			return true;
	}
	
	function get_content_types($id)
	{
		$types = $this->db->query('select * from content_types where bricabox_id = ? order by position asc', array($id));
		return $types->num_rows() ? $types : false;
	}
	
	function get_categories($id)
	{
		$ct = $this->db->query('select * from categories where bricabox_id = ? order by name asc', array($id));
		return $ct->num_rows() ? $ct : false;
	}
	
	function create_new_content_type($id, $params)
	{
		$CI =& get_instance();
		$CI->load->model('Content_model');
		
		// check for name conflict
		$existing = $CI->Content_model->get_content_type_by_name($params['name'], $id);
		if ($existing)
			return false;
		else
		{
			// save the thing
			$params['bricabox_id'] = $id;
			if ($this->db->insert('content_types', $params))
				return true;
		}
	}
	
	function save_content_type($id, $params, $current_name)
	{
		global $BBX;
		
		$CI =& get_instance();
		$CI->load->model('Content_model');
		
		// check for name conflict (only if the name was changed)
		if ($current_name != $params['name'])
			$existing = $CI->Content_model->get_content_type_by_name($params['name'], $id);
		else
			$existing = false;
			
		if ($existing)
			return false;
		else
		{
			// save the thing
			$params['bricabox_id'] = $id;
			
			$mc_key = "content_type_{$params['id']}";
			
			$this->db->where('id', $params['id']);
			unset($params['id']); // make sure a SET id = ? doesn't make it into the SQL
			
			// disable any fields the user unchecked; they won't come thru $_POST so we have to set them manually
			foreach (array('file_upload', 'url', 'address', 'subtitle', 'datetime') as $field)
			{
				$name = "input_use_{$field}";
				if (!isset($params[$name]))
					$params[$name] = 0;
			}
			
			// clear memcached
			$has_mc = ($BBX['memcache'] === false) ? false : true;
			if ($has_mc)
			{
				log_message('debug', "memcache: clearing content_type at {$mc_key}");
				$BBX['memcache']->delete($mc_key);
			}
			
			if ($this->db->update('content_types', $params))
				return true;
		}
	}
	
	function count_content_types($bricabox_id)
	{
		$count_query = $this->db->query('select count(*) as count_all from content_types where bricabox_id = ?', array($bricabox_id));
		return $count_query->first_row()->count_all;
	}
	
	function record_pageview($bricabox_id)
	{
		$this->db->query('update bricaboxes set stat_total_views = stat_total_views + 1 where id = ?', array($bricabox_id));
	}
	
	function block_excluded($id, $ct_id)
	{
		$query = $this->db->query('select * from block_exclusions where block_instance_id = ? and content_type_id = ? limit 1', array($id, $ct_id));
		if ($query->num_rows() == 1)
			return true;
	}
}
?>